package com.lancoo.znbkxx.teacher.model.api

import com.lancoo.znbkxx.teacher.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.internal.addHeaderLenient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.concurrent.TimeUnit

/**
 * @author : Admin
 * @date   : 2019/7/11 10:00
 * @desc   :
 */
abstract class RetrofitClient {

    companion object {
        private const val TIME_OUT = 180
    }

    private val client: OkHttpClient
        get() {
            val builder = OkHttpClient.Builder()
            val logging = HttpLoggingInterceptor()
            if (BuildConfig.DEBUG) {
                logging.level = HttpLoggingInterceptor.Level.BODY
            } else {
                logging.level = HttpLoggingInterceptor.Level.BASIC
            }
            builder.addInterceptor(logging).addInterceptor(LogInterceptor())
                    .readTimeout(TIME_OUT.toLong(), TimeUnit.SECONDS)
                    .connectTimeout(TIME_OUT.toLong(), TimeUnit.SECONDS)
            return builder.build()
        }


    fun <S> getService(serviceClass: Class<S>, baseUrl: String): S {
        return Retrofit.Builder()
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .addCallAdapterFactory(CoroutineCallAdapterFactory.invoke())
                .baseUrl(baseUrl)
                .build().create(serviceClass)
    }
}